package com.site.service.impl.dining;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.site.dao.dining.RestaurantDao;
import com.site.dao.dining.RestaurantPhotoDao;
import com.site.dto.dining.RestaurantBizBean;
import com.site.model.dining.RestaurantInfo;
import com.site.model.dining.RestaurantPhotoInfo;
import com.site.service.dining.RestaurantService;
import com.site.util.PropertyUtils;
import com.site.util.base.businessbean.FileBean;
import com.site.util.base.service.BaseService;
import com.site.util.common.fileupload.FileUploadHandle;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

/**
 * 餐厅Service
 * @author jack
 *
 */
@Service
public class RestaurantServiceImpl implements RestaurantService {
	
	private Log log=LogFactory.getLog(this.getClass());

	private RestaurantDao restaurantDao;
	
	private RestaurantPhotoDao restaurantPhotoDao;
	
	@Resource
	public void setRestaurantPhotoDao(RestaurantPhotoDao restaurantPhotoDao) {
		this.restaurantPhotoDao = restaurantPhotoDao;
	}

	@Resource
	public void setRestaurantDao(RestaurantDao restaurantDao) {
		this.restaurantDao = restaurantDao;
	}

	@Override
	public List<RestaurantBizBean> getRestaurants(QueryPage queryPage, RestaurantBizBean restaurantBizBean){
		List<RestaurantInfo> restaurantInfos = restaurantDao.getRestaurants(queryPage, restaurantBizBean);
		if(null != restaurantInfos && !restaurantInfos.isEmpty()){
			List<RestaurantBizBean> restaurantBizBeans = new ArrayList<RestaurantBizBean>();
			for (RestaurantInfo restaurantInfo : restaurantInfos) {
				if(null != restaurantInfo){
					RestaurantBizBean restaurantBizBean2 =  new RestaurantBizBean();
					PropertyUtils.copyProperties(restaurantBizBean2, restaurantInfo);
					restaurantBizBeans.add(restaurantBizBean2);
				}
			}
			return restaurantBizBeans;
		}
		return null;
	}

	@Override
	public Object execute(Object obj, String command) throws SystemException,
			ApplicationException {
		// 增加
		if(BaseService.COMMAND_ADD.equals(command)){
			add((Map<String, Object>)obj);
		} else if(BaseService.COMMAND_UPDATE.equals(command)){
			update((RestaurantBizBean)obj);
		}
		
		return null;
	}

	/**
	 * 更新
	 * @param obj
	 */
	private void update(RestaurantBizBean obj) {
		if(null != obj && obj.getId() != null){
			RestaurantInfo restaurantInfo = restaurantDao.getEntityById(RestaurantInfo.class, obj.getId());
			PropertyUtils.copyNotNullProperties(restaurantInfo, obj);
			restaurantDao.update(restaurantInfo);
		}
	}

	/**
	 * 增加
	 * @param map
	 */
	private void add(Map<String, Object> map) {
		if(map != null && !map.isEmpty()){
			RestaurantBizBean restaurantBizBean = (RestaurantBizBean)map.get("restaurantBizBean");
			RestaurantInfo restaurantInfo = new RestaurantInfo();
			PropertyUtils.copyProperties(restaurantInfo,restaurantBizBean);
			restaurantInfo.setRno(restaurantDao.getMaxId("CT", 4, RestaurantInfo.class));
			restaurantDao.save(restaurantInfo);
			// 上传图片
			FileUploadHandle fileUploadHandle = (FileUploadHandle)map.get("fileUploadHandle");
			if(null != fileUploadHandle){
				// 上传路径[主图]目录
				String mainPath = "/restaurant/"+restaurantInfo.getRno()+"/main";
				fileUploadHandle.setUploadDir(getUploadPath(fileUploadHandle.getRequest(), mainPath));
				List<FileBean> fileBeans = fileUploadHandle.handleUploadFile();
				
				if(null != fileBeans && !fileBeans.isEmpty()){
					for (FileBean fileBean : fileBeans) {
						if(null != fileBean && !StringUtils.isEmpty(fileBean.getNewName())){
							RestaurantPhotoInfo restaurantPhotoInfo = new RestaurantPhotoInfo();
							restaurantPhotoInfo.setCreatedDate(new Date());
							restaurantPhotoInfo.setRid(restaurantInfo.getId());
							restaurantPhotoInfo.setPath("/"+restaurantInfo.getRno()+"/main/"+fileBean.getNewName());
							restaurantPhotoDao.save(restaurantPhotoInfo);
							log.info("餐厅主图  "+fileBean.getNewName()+" 正在上传…");
						}
					}
				}
			}
		}
	}
	
	/**
	 * 获得上传路径
	 * @param request
	 * @return
	 */
	public static String getUploadPath(HttpServletRequest request, String path){
		// 主图
		String myUploadDir = request.getSession().getServletContext().getRealPath(path);
		// Constants.IMAGE_UPLOAD_PATH;
		File userIdDirFile = new File(myUploadDir);
		if(!(userIdDirFile.exists())&&!(userIdDirFile.isDirectory())){
           boolean creadok = userIdDirFile.mkdirs();
           if (creadok) {
        	   myUploadDir=myUploadDir+"/";
           }
        }else{
        	myUploadDir=myUploadDir+"/";
        }
        return myUploadDir;
	}

	@Override
	public RestaurantBizBean getRestaurantById(RestaurantBizBean restaurantBizBean) {
		if(null != restaurantBizBean && restaurantBizBean.getId() != null){
			RestaurantInfo restaurantInfo = restaurantDao.getEntityById(RestaurantInfo.class, restaurantBizBean.getId());
			PropertyUtils.copyProperties(restaurantBizBean, restaurantInfo);
			return restaurantBizBean;
		}
		return null;
	}
}
